package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.app.bean.SharePrefKeys;

/**
 * Created by lforxeverc on 2016/11/24.
 */

public class PhoneBaseEditTextView extends EditText {

    public PhoneBaseEditTextView(Context context) {
        this(context, null);
    }

    public PhoneBaseEditTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public PhoneBaseEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFocusable(true);
        setFocusableInTouchMode(true);
        addTextChangedListener(new TextWatcher() {
            boolean isDelete;//删除状态
            int lastLength;//上次的字符长度
            int selection = 0;//光标位置
            boolean shouldBlock = false;//是否屏蔽回调

            @Override
            public void beforeTextChanged(CharSequence beforeStr, int startAt, int count, int after) {
                isDelete = lastLength > beforeStr.length();
                lastLength = beforeStr.length();
            }

            @Override
            public void onTextChanged(CharSequence changedStr, int startAt, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable changedStr) {
                if (shouldBlock) {
                    shouldBlock = false;
                    if (!isDelete && (selection == 4 || selection == 9)) selection++;
                    setSelection(selection > changedStr.length() ? changedStr.length() : selection);
                    return;
                }
                selection = getSelectionStart();
                String trimStr = changedStr.toString().replace(" ", "");
                if (trimStr.length() <= 3) {
                    shouldBlock = true;
                    setText(trimStr);
                } else if (trimStr.length() <= 7) {
                    shouldBlock = true;
                    setText(trimStr.substring(0, 3) + " " + trimStr.substring(3, trimStr.length()));

                } else {
                    shouldBlock = true;
                    setText(trimStr.substring(0, 3) + " " + trimStr.substring(3, 7) + " " + trimStr.substring(7, trimStr.length()));
                }

            }
        });
        setText(getPhoneNumFromCache());
    }

    public void savePhoneNum() {
        PreferenceManager.setData(SharePrefKeys.PREF_USER_PHONE, getRealPhoneNum());
    }

    private String getPhoneNumFromCache() {
        return PreferenceManager.getStringData(SharePrefKeys.PREF_USER_PHONE);
    }

    public String getRealPhoneNum() {
        String text = getText().toString();
        if (!TextUtils.isEmpty(text)) {
            text = text.replace(" ", "");
        }
        return text;
    }

}
